<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="15">
            <a-form-item label="检索词">
              <a-input placeholder="请输入检索词" v-model="queryParam.searchKey"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item label="索引类型">
              <a-select
                placeholder="请选择索引类型"
                v-model="queryParam.type"
              >
                <a-select-option v-for="type in typeOptions" :key="type.key" :value="type.key">{{
                  type.value
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" @click="detailHandler()" :disabled="this.selectedRowKeys.length != 1">查看</a-button>
      <a-button @click="$refs.reIndexModal.reIndex()" v-priv="'ElasticSearch.IndicesManager.IndexRebuild'">索引重建</a-button>
      <a-button @click="reIndexAllHandler()" v-priv="'ElasticSearch.IndicesManager.IndexRebuild'">全库索引重建</a-button>
      <a-button @click="releaseIndexLockHandler()" v-priv="'ElasticSearch.IndicesManager.IndexReleaseLock'">锁释放</a-button>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="id"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :columns="columns"
      :data="loadData"
    >
      <a slot="_id" slot-scope="text,record" @click="$refs.indexDetailModal.info(record.id, record.type)">{{ record._id }}</a>
    </s-table>
    <re-index-modal ref="reIndexModal" />
    <index-detail-modal ref="indexDetailModal" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { indexList, indicesTypes, reIndexAll, releaseIndexLock } from '@/api/system'
import ReIndexModal from '@/views/system/components/ReIndexModal'
import IndexDetailModal from '@/views/system/components/IndexDetailModal'
export default {
  name: 'IndicesManager',
  components: {
    STable,
    ReIndexModal,
    IndexDetailModal
  },
  data () {
    return {
      description: '',
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      typeOptions: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 表头
      columns: [
        {
          title: '_id',
          dataIndex: 'id',
          scopedSlots: { customRender: '_id' }
        },
        {
          title: '_type',
          dataIndex: 'type'
        },
        {
          title: '标题',
          dataIndex: 'title',
          ellipsis: true
        },
        {
          title: '简介',
          dataIndex: 'summary',
          ellipsis: true
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return indexList(Object.assign(parameter, {
          searchKey: this.queryParam.searchKey ? this.queryParam.searchKey.trim() : '',
          type: this.queryParam.type ? this.queryParam.type.trim() : 'all'
        }))
      }
    }
  },
  created () {
    indicesTypes({}).then(res => {
      this.typeOptions = res.data
    })
  },
  methods: {
    onCreateTimeChange (value, dateString) {
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    detailHandler () {
      if (this.selectedRows.length === 1) {
        this.$refs.indexDetailModal.info(this.selectedRows[0].id, this.selectedRows[0].type)
      }
    },
    reIndexAllHandler () {
      const that = this
      this.$confirm({
        title: '确定重建全库索引？？？',
        onOk () {
          reIndexAll({}).then(res => {
            if (res.status === 1) {
              that.$message.success(res.message)
            } else {
              that.$message.error(res.message)
            }
          })
        }
      })
    },
    releaseIndexLockHandler () {
      const that = this
      this.$confirm({
        title: '确认释放索引锁？',
        onOk () {
          releaseIndexLock({}).then(res => {
            if (res.status === 1) {
              that.$message.success(res.message)
            } else {
              that.$message.error(res.message)
            }
          })
        }
      })
    }
  },
  watch: {
  }
}
</script>
